(function(){
	var bool = false;
	var currentlevel = new Object();
	currentlevel.index = 0;
	currentlevel.Matrix = levels[0].Matrix;
	currentlevel.position_depart = levels[0].position_depart;
	var movements_counter = 0;
	var newinputs = new Array();
	var objectiveCase = false;
	var pixels = 34;
	var sokoban = document.getElementById('sokoban');
	var span_counter = document.getElementById('movement_number');
	var context = sokoban.getContext('2d');
	var mario = {
		position : {
				x:currentlevel.position_depart.x*34,
				y:currentlevel.position_depart.y*34
		},
		orientation : 8	
	};
	var won = false;
	var winInterval;
	
	currentlevel.width = currentlevel.Matrix[0].length;
	currentlevel.height = currentlevel.Matrix.length;
	currentlevel.Matrix = adaptLevel(currentlevel.Matrix);
	drawLevel(currentlevel.Matrix);
	
	//Evénement clavier
	window.addEventListener('keydown',function(keyEv){
		if(won == false){
			var previous = {
				x : mario.position.x,
				y : mario.position.y,
			}
			var movement = true;
			switch(keyEv.keyCode){
				case 40:
					mario.position.y += pixels;
					mario.orientation = 8;
				break;
				case 39:
					mario.position.x += pixels;
					mario.orientation = 7;
				break;
				case 38:
					mario.position.y -= pixels;
					mario.orientation = 6;
				break;
				case 37:
					mario.position.x -= pixels;
					mario.orientation = 5;
				break;
				default:
					movement = false;			
			}
			var newPos = whatIsThis(mario.position.x,mario.position.y);
			var deltaX = mario.position.x - previous.x;
			var deltaY = mario.position.y - previous.y;
			var newBoxPos = whatIsThis(mario.position.x+deltaX,mario.position.y+deltaY);
			if((newPos != -1 && newPos != 1 && newPos != 4) && !(newPos == 2 && (newBoxPos == -1 || newBoxPos == 1 || newBoxPos == 2)) && movement){	
				reloadImg(mario.position.x/pixels,mario.position.y/pixels,mario.orientation);		
				if(objectiveCase){
					reloadImg(previous.x/pixels,previous.y/pixels,3);
					objectiveCase = false;
				}
				else{
					reloadImg(previous.x/pixels,previous.y/pixels,0);
				}
				if(newPos == 2){
					if(whatIsThis(mario.position.x+deltaX,mario.position.y +deltaY) == 3){
						reloadImg(mario.position.x/pixels + deltaX/pixels,mario.position.y/pixels + deltaY/pixels,4);
						if(winOrNot()){
							win();
						}
					}
					else{
						reloadImg(mario.position.x/pixels + deltaX/pixels, mario.position.y/pixels + deltaY/pixels,2);
					}
				}
				if(newPos == 3){
					objectiveCase = true;
				}
				span_counter.innerHTML = ++movements_counter;
				

			}
			else{
				mario.position.x = previous.x;
				mario.position.y = previous.y;
			}
		}
	},false);
	//Bouton restart
	document.getElementById('button_restart').addEventListener('click',function(){
		reloadImg(mario.position.x/34,mario.position.y/34,0);
		currentlevel.Matrix = adaptLevel(currentlevel.Matrix);
		loadLevel(currentlevel.index);
	},false)

	function adaptLevel(matrix){
		var newmatrix = new Array();
		for( var i =0;i <= matrix[0].length - 1;i++){
			newmatrix[i] = new Array();
			for( var j = 0; j <= matrix.length - 1;j++){
				newmatrix[i][j] = matrix[j][i];
			}
		}
		return newmatrix;
	}
	
	function drawLevel(){
		sokoban.setAttribute('width',currentlevel.width*pixels);
		sokoban.setAttribute('height',currentlevel.height*pixels);
		for(var i = 0; i <= currentlevel.width - 1;i++){
			for(var j = 0;j <= currentlevel.height - 1;j++){
				reloadImg(i,j,currentlevel.Matrix[i][j],1);
			}
		}
		mario_img = new Image();
		mario_img.src="./images/mario_bas.gif";
		reloadImg(mario.position.x/pixels,mario.position.y/pixels,8);
	}

	function emptyRect(x,y){
		context.fillStyle = "#FFFFFF";
		context.fillRect(x,y,pixels,pixels);
		context.beginPath();
		context.moveTo(x,y);
		context.lineTo(x+pixels,y);
		context.lineTo(x+pixels,y+pixels);
		context.lineTo(x,y+pixels);
		context.closePath();
		context.stroke();
	}
	
	function loadLevel(levelNumber){
		currentlevel = new Object();
		currentlevel.index = levelNumber;
		currentlevel.Matrix = levels[levelNumber].Matrix;
		currentlevel.position_depart = levels[levelNumber].position_depart;
		currentlevel.width = currentlevel.Matrix[0].length;
		currentlevel.height = currentlevel.Matrix.length;
		mario.position.x = currentlevel.position_depart.x*34;
		mario.position.y = currentlevel.position_depart.y*34;
		currentlevel.Matrix = adaptLevel(currentlevel.Matrix);
		drawLevel(currentlevel.Matrix);
		movements_counter = 0;
		span_counter.innerHTML = movements_counter;
		windiv = document.getElementById('win');
		if(windiv){
			document.body.removeChild(windiv);
		}
		won = false;
		clearInterval(winInterval);
		levelsdiv.style.left = sokoban.width + 10 + "px";
	}

	function reloadImg(coorx,coory,newValue,draw)
	{
		emptyRect(coorx*pixels,coory*pixels);
		if(newValue != 0){
			newimg = new Image();
			switch(newValue){
				case 1:
					newimg.src = "./images/mur.jpg";
				break;
				case 2:
					newimg.src = "./images/caisse.jpg";
				break;
				case 3:
					newimg.src = "./images/objectif.png";
				break;
				case 4:
					newimg.src = "./images/caisse_ok.jpg";
				break;
				case 5:
					newimg.src = "./images/mario_gauche.gif";
				break;
				case 6:
					newimg.src = "./images/mario_haut.gif";
				break;
				case 7:
					newimg.src ="./images/mario_droite.gif";
				break;
				case 8:
					newimg.src = "./images/mario_bas.gif";
				break;
			}
			context.drawImage(newimg,coorx*pixels,coory*pixels);

			
		}
		if(!draw)
			currentlevel.Matrix[coorx][coory] = newValue;
	}
	
	function whatIsThis(x,y){
		if(x >= pixels*currentlevel.width || x < 0 || y < 0 || y >= pixels*currentlevel.height){
			return -1;
		}
		else{
			return currentlevel.Matrix[x/pixels][y/pixels];
		}
	}

	function win(){
		var windiv = document.createElement('div');
		windiv.setAttribute('id','win');
		document.body.appendChild(windiv);
		var winvid = document.createElement('video');
		winvid.src = 'images/RickRoll.ogv';
		winvid.setAttribute('autoplay','autoplay');
		windiv.appendChild(winvid);
		var winspan = document.createElement('span');
		winspan.setAttribute('id','winspan');
		windiv.appendChild(winspan);
		winInterval = setInterval(function(){
			if(bool){
				winspan.innerHTML = '';
				bool = false;
			}else{
				winspan.innerHTML = "C'est gagnééééééééééééééééééééééééééééééééé";
				bool = true;
			}
		},200);
		won = true;
		
		
	}
	function winOrNot()
	{
		for(var i = 0;i <= currentlevel.width-1;i++){
			if(currentlevel.Matrix[i].indexOf(3) != -1){
				return false;
			}
		}
		return true;	
	}

	var levelsdiv = document.createElement('div');
	levelsdiv.setAttribute('id','levelsdiv');
	document.body.appendChild(levelsdiv);
	levelsdiv.style.position = "absolute";
	levelsdiv.style.top = 0;
	levelsdiv.style.left = sokoban.width + 10 + "px";
	
	
	for(var i = 0; i <= levels.length - 1; i++){
		if( i % 5 == 0){	
			levelsdiv.appendChild(document.createElement('br'));
		}
		newinputs[i] = document.createElement('input');
		newinputs[i].setAttribute('id',i);
		newinputs[i].setAttribute('type','button');
		newinputs[i].setAttribute('value','Niveau ' + (i + 1));
		newinputs[i].classList.add('levelbutton');
		newinputs[i].addEventListener('click',function(clickev){
			reloadImg(mario.position.x/34,mario.position.y/34,0);
			currentlevel.Matrix = adaptLevel(currentlevel.Matrix);
			loadLevel(clickev.target.id);
		},false);
		levelsdiv.appendChild(newinputs[i]);
	}
})();

